//
//  CustomSearchBar.m
//  GOtrip
//
//  Created by henry on 01/02/2012.
//  Copyright (c) 2012 广东薪传 . All rights reserved.
//

#import "CustomSearchBar.h"
#import <QuartzCore/QuartzCore.h>
@implementation CustomSearchBar

- (id) initWithFrame:(CGRect)frame{
	if(self=[super initWithFrame:frame]){
		self.frame=frame;
	}
	
	return self;
}
-(void) layoutSubviews{
    UITextField *searchField = nil;
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
            searchField = (UITextField *)subview;
            break;
        }
    }
    // 设置搜索栏输入框边框颜色
     searchField.layer.cornerRadius = 8.0f;
     searchField.layer.masksToBounds = YES;
     //searchField.layer.borderColor = [[UIColor colorWithRed:118.0f/255.0f green:193.0f/255.0f blue:220.0f/255.0f alpha:1.0f]CGColor];
     //searchField.layer.borderWidth = 2.0f;
    [super layoutSubviews];
}


- (void)setBarBackgroundImage:(UIImage *)backgroundImage{   
    if ([[[UIDevice currentDevice] systemVersion] doubleValue]<5.0) {     
        UIImageView *imageVIew = [[UIImageView alloc] initWithFrame:CGRectZero];       
        [imageVIew setImage:backgroundImage];   
        imageVIew.bounds = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);  
        NSArray *subs = self.subviews;       
        for (int i = 0; i < [subs count]; i++) {           
            id subv = [self.subviews objectAtIndex:i];           
            if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){                            
                [imageVIew setFrame:imageVIew.bounds];               
                [self insertSubview:imageVIew atIndex:i]; 
                //[subv removeFromSuperview];  
            }       
        }   
        [imageVIew setNeedsDisplay];       
        [imageVIew setNeedsLayout]; 
        [imageVIew release];
    }else{
        [self setBackgroundImage:backgroundImage];
    }
} 

- (void)setImage:(UIImage *)iconImage{
        UITextField *searchField = nil;
        for (UIView *subview in self.subviews) {
            if ([subview isKindOfClass:[UITextField class]]) {
                searchField = (UITextField *)subview;
                break;
            }
        }
        if (searchField) {
            searchField.borderStyle = UITextBorderStyleRoundedRect;
            UIImageView *iView = [[UIImageView alloc] initWithImage:iconImage];
            searchField.leftView = iView;
            [iView release];
        }

    
}

@end
